package dbu.dbase.charset;

import java.nio.charset.Charset;
import java.nio.charset.spi.CharsetProvider;
import java.util.HashSet;
import java.util.Iterator;

/**
 * A CharsetProvider class which makes available the MIK charsets.
 *
 * To activate this CharsetProvider, it's necessary to add a file to
 * the classpath of the JVM runtime at the following location:
 *   META-INF/services/java.nio.charsets.spi.CharsetProvider
 *
 * That file must contain a line with the fully qualified name of
 * this class on a line by itself:
 *   dbu.dbase.charset.MIKCharsetProvider
 *
 * See the javadoc page for java.nio.charsets.spi.CharsetProvider
 * for full details.
 *
 */
public class MIKCharsetProvider extends CharsetProvider {
	// The name of the charset we provide
	private static final String CHARSET_NAME = "MIK";

	// A handle to the Charset object
	private Charset mik = null;

	/**
	 * Constructor, instantiate a Charset object and save the reference.
	 */
	public MIKCharsetProvider() {
		this.mik = new MIKCharset();
	}

	/**
	 * Called by Charset static methods to find a particular named
	 * Charset.  If it's the name of this charset (we don't have
	 * any aliases) then return the MIK Charset, else return null.
	 */
	@Override
	public Charset charsetForName(String charsetName) {
		if (charsetName.equalsIgnoreCase(CHARSET_NAME)) {
			return (mik);
		}
		return (null);
	}

	/**
	 * Return an Iterator over the set of Charset objects we provide.
	 * @return An Iterator object containing references to all the
	 *  Charset objects provided by this class.
	 */
	@Override
	public Iterator<Charset> charsets() {
		HashSet<Charset> set = new HashSet<Charset>(1);

		set.add(mik);

		return (set.iterator());
	}
}
